<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<cl-filter label="类型">
				<cl-select :options="typeList" prop="type" style="width: 120px" />
			</cl-filter>
			<cl-filter label="状态">
				<cl-select :options="statusList" prop="status" style="width: 120px" />
			</cl-filter>
			<cl-filter label="应用">
				<cl-select :options="appList" prop="appId" style="width: 120px" />
			</cl-filter>
			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key field="topic" :field-list="[
				{
					label: 'Topic',
					value: 'topic'
				},
				{
					label: '应用名称',
					value: 'name'
				}
			]" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="task-topic" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { onMounted, ref } from "vue";
import { fa } from "element-plus/es/locale";

const { service } = useCool();

//状态
const typeList = ref([
	{ label: "HTTP", value: 0 },
	{ label: "MQTT", value: 1, color: "#67C23A" }
]);

//状态
const statusList = ref([
	{ label: "禁用", value: 0 },
	{ label: "启用", value: 1, color: "#67C23A" }
]);

const appList = ref([]);

onMounted(() => {
	//应用名称下拉项
	service.task.app.list().then((res) => {
		appList.value = res.map((item) => {
			return { ...item, label: item.name, value: item.id };
		});
	});
});

// cl-upsert
const Upsert = useUpsert({
	onOpened(data) {
		service.task.app.list().then((res) => {
			const result: any = res.map((item) => {
				return { ...item, label: item.name, value: item.id };
			});
			Upsert.value?.setOptions("appId", result);
		});
		Upsert.value?.hideItem(data.type == 1 ? "HTTPUrl" : "MQTTUrl");
		data.connectOptions = data.connectOptions ? JSON.stringify(data.connectOptions) : null;
		Upsert.value?.setProps("name", { disabled: data.id ? true : false });
	},
	onInfo(data, { next }) {
		Upsert.value?.hideItem(data.type == 1 ? "HTTPUrl" : "MQTTUrl");
		next(data);
	},
	items: [
		{ prop: "appId", label: "应用名称", required: true, component: { name: "el-select" } },
		{
			prop: "name",
			label: "名称",
			component: {
				name: "el-input"
			},
			rules: [
				{
					required: true,
					trigger: "blur",
					pattern: /^[a-zA-Z0-9_]+$/,
					message: "必须是英文、数字或者下划线"
				}
			]
		},
		{
			flex: false,
			prop: "status",
			value: 1,
			label: "启用状态",
			component: { name: "cl-switch" },
			required: true
		},
		{
			prop: "type",
			label: "类型",
			value: 0,
			component: {
				name: "el-radio-group",
				options: typeList.value,
				props: {
					onChange: (val: number) => {
						Upsert.value?.["hideItem"](val == 0 ? "MQTTUrl" : "HTTPUrl");
						Upsert.value?.["showItem"](val == 1 ? "MQTTUrl" : "HTTPUrl");
						Upsert.value?.setForm(val == 1 ? "MQTTUrl" : "HTTPUrl", "");
					}
				},
				value: 0,
				required: true
			}
		},
		{
			prop: "HTTPUrl",
			label: "HTTP请求地址",
			component: { name: "el-input" }
		},
		{
			prop: "MQTTUrl",
			label: "MQTT请求地址",
			component: { name: "el-input" }
		},
		{
			prop: "connectOptions",
			label: "连接参数",
			component: {
				name: "el-input",
				props: { type: "textarea", rows: 8, placeholder: "请输入连接参数，必须为JSON格式" }
			}
		},
		{
			prop: "remark",
			label: "备注",
			component: { name: "el-input", props: { type: "textarea", rows: 5 } }
		}
	],
	props: {
		labelWidth: 140
	}
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ prop: "appName", label: "应用名称" },
		{ prop: "name", label: "Topic名称" },
		{ prop: "id", label: "ID" },
		{ prop: "appId", label: "应用ID" },
		{ prop: "status", label: "启用状态", component: { name: "cl-switch" } },
		{
			prop: "type",
			label: "类型",
			dict: [
				{ label: "HTTP", value: 0 },
				{ label: "MQTT", value: 1, color: "#67C23A" }
			]
		},
		// { prop: "HTTPUrl", label: "HTTP请求地址", component: { name: "cl-link" } },
		// { prop: "MQTTUrl", label: "MQTT请求地址", component: { name: "cl-link" } },
		// { prop: "connectOptions", label: "连接参数" },
		{ prop: "remark", label: "备注", showOverflowTooltip: true },
		{ prop: "createTime", label: "创建时间", sortable: "desc", width: 160 },
		{ prop: "updateTime", label: "更新时间", sortable: "custom", width: 160 },
		{ type: "op", buttons: ["edit", "delete"] }
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.task.topic
	},
	(app) => {
		app.refresh();
	}
);
</script>
